package services;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import play.cache.Cache;
import play.libs.Codec;
import utils.BizConstants;
import utils.PageUtil;
import vos.SystemOperatorVo;
import models.SystemLog;
import models.SystemOperator;
import models.SystemParams;
import models.SystemParamsExample;
import models.mapper.SystemLogMapper;
import models.mapper.SystemOperatorMapper;
import models.mapper.SystemParamsMapper;

/**
 * 系统参数服务类
 * @author wangshaoming
 *
 */
public class SystemParamService {
	/**
	 * 从缓存中获取对象
	 * @param key
	 * @return
	 */
	public static String getValueByCacheId(String key){
		String paramValue=(String)Cache.get(key+BizConstants._PARAMS);
		if(StringUtils.isNotEmpty(paramValue)){
			return paramValue;
		}
		return getById(key)==null?"":getById(key).getParamvalue();
	}
	/**
	 * 根据参数编号获取参数对象
	 * @param key 参数编号
	 * @return
	 */
	public static SystemParams getById(String key){
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			SystemParamsMapper mapper=ss.getMapper(SystemParamsMapper.class);
			return mapper.selectByPrimaryKey(key);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取参数列表
	 * @return
	 */
	public static List<SystemParams> getParams() {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			SystemParamsMapper mapper=ss.getMapper(SystemParamsMapper.class);
			SystemParamsExample example=new SystemParamsExample();
			example.setOrderByClause("paramid asc");
			return mapper.selectByExample(example);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据分类获取参数
	 * @param sort
	 * @return
	 */
	public static List<SystemParams> getParamsBySort(String sort) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			SystemParamsMapper mapper=ss.getMapper(SystemParamsMapper.class);
			SystemParamsExample example=new SystemParamsExample();
			SystemParamsExample.Criteria c=example.createCriteria();
			c.andSortEqualTo(sort);
			return mapper.selectByExample(example);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取参数列表
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param searchText
	 * @return
	 */
	public static PageInfo<SystemParams> getList(int page, int rows,
			String sort, String order, String searchText) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			SystemParamsMapper mapper=ss.getMapper(SystemParamsMapper.class);
			PageHelper.startPage(page, rows,PageUtil.joinSortFieldOrder(sort, order));
			return new PageInfo<SystemParams>(mapper.getList(searchText));
		}finally{
			ss.close();
		}
	}
	/**
	 * 更新参数值
	 * @param param
	 * @param ip 
	 * @param oper 
	 * @return
	 */
	public static boolean updateParam(SystemParams param, SystemOperator oper, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			SystemParamsMapper mapper=ss.getMapper(SystemParamsMapper.class);
			mapper.updateByPrimaryKey(param);
			//记录日志
			SystemLog log=new SystemLog(Codec.UUID(),oper.getOperatorid(),ip,"修改参数","修改参数"+param.getParamid()+",修改为"+param.getParamvalue(),new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
}
